﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using Writespace.Dialogs;

using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;


namespace WritespaceDriver
{
    public class Program
    {
        private static string filename = "";

        [STAThread]
        public static void Main(string[] args)
        {

            try
            {
                var editor = new TextEditorWindow();

                if (args.Length > 0)
                {
                    filename = args[0];
                    LoadFileIntoEditor(editor);
                }

                editor.Open += ((sender, e) => Open(editor));
                editor.Save += ((sender, e) => Save(editor));

                editor.Closing += ((sender, e) =>
                {
                    if (editor.IsModified)
                    {
                        var result = MessageBox.Show("Do you want to save?", "Save?", MessageBoxButton.YesNo, MessageBoxImage.Question);

                        if (result == MessageBoxResult.Yes) Save(editor);
                    }
                });

                editor.WindowState = WindowState.Maximized;
                var app = new Application();
                app.Run(editor);

            }
            catch (Exception e)
            {
                MessageBox.Show("" + e, "Error",MessageBoxButton.OK,MessageBoxImage.Error);
            }
        }

        private static void Open(TextEditorWindow editor)
        {
            var dialog = new OpenFileDialog();

            dialog.DefaultExt = "txt";
            dialog.Filter = "Text files (*.txt)|*.txt";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                filename = dialog.FileName;
                LoadFileIntoEditor(editor);
            }
            editor.Focus();
        }


        private static void LoadFileIntoEditor(TextEditorWindow editor)
        {
            var content = "";
            try
            {
                using (var sr = new StreamReader(filename))
                    content = sr.ReadToEnd();
            }
            catch (Exception)
            {
                var result = MessageBox.Show("File does not exist, create a new one?", "Create new file?", MessageBoxButton.YesNo,
                                             MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                    using (new StreamWriter(filename, false)) { }
            }
            editor.Text = content;
            editor.IsModified = false;
            editor.Status = filename + " loaded";
        }

        private static void Save(TextEditorWindow editor)
        {
            if (!string.IsNullOrEmpty(filename))
            {
                SaveFile(editor);
            }
            else
                SaveFileWithDialog(editor);

            editor.Focus();
        }

        private static void SaveFile(TextEditorWindow editor)
        {
            using (var sw = new StreamWriter(filename, false))
                sw.Write(editor.Text);
            editor.IsModified = false;
            editor.Status = "Saved " + filename;
        }

        private static void SaveFileWithDialog(TextEditorWindow editor)
        {
            var dialog = new SaveFileDialog();
            dialog.DefaultExt = "txt";
            dialog.Filter = "Text files (*.txt)|*.txt";
            
            if (dialog.ShowDialog() != DialogResult.OK) return;
            filename = dialog.FileName;
            SaveFile(editor);
        }
    }
}
